package com.yu.transport.client;

import com.yu.transfer.RpcResponse;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @PackageName: com.yu.transport.client
 * @Description: 服务端没有处理的请求,使用一个map存储
 *
 * @Version: v1.0.0
 * @Author: yuweilin
 * @Date: 17:02
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2021/2/5      yuweilin         v1.0.0               新建
 */
public class UnprocessedRequests {
    private static final Map<String, CompletableFuture<RpcResponse<Object>>> UNPROCESSED_REQUESTS = new ConcurrentHashMap<>();

    public void put(String requestId,CompletableFuture<RpcResponse<Object>> future){
        UNPROCESSED_REQUESTS.put(requestId,future);
    }

    public void complete(RpcResponse<Object> rpcResponse){
        CompletableFuture<RpcResponse<Object>> future = UNPROCESSED_REQUESTS.remove(rpcResponse.getRequestId());
        if (null!=future){
            future.complete(rpcResponse);
        }else{
            throw new IllegalStateException();
        }
    }

}
